/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 商店模块
	/// module : shop
	/// </summary>
	public interface SNetShop : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.shop_buy, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.shop_buy, "Recv->" + data.ToString());
#endif
		                BuyRequest _data = (BuyRequest)data;
						return CS_buy(session, _data.shopCid, _data.index);
		            } 
            	},
                { NetMessageId.shop_refresh, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.shop_refresh, "Recv->" + data.ToString());
#endif
		                RefreshRequest _data = (RefreshRequest)data;
						return CS_refresh(session, _data.shopCid, _data.type);
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.shop_buy, ()=> new BuyRequest()},
	        	{NetMessageId.shop_refresh, ()=> new RefreshRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        /// 商店购买，index=位置，从1开始
        /// </summary>
	    protected BuyResult CS_buy(ISession session, int shopCid, int index);
		/// <summary>
        /// 刷新商店,type=1免费刷新2道具刷新3付费刷新
        /// </summary>
	    protected RefreshResult CS_refresh(ISession session, int shopCid, int type);
#endregion
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
        public BuyResult SC_buyResult(Task<BuyResult> future){return new BuyResult(future);}
        public BuyResult SC_buyResult(int code, int shopCid, int index){return new BuyResult(code, shopCid, index);}
        public BuyResult SC_buyResult(int code){return new BuyResult(code);}
		/// <summary>
        /// 
        /// </summary>
        public RefreshResult SC_refreshResult(Task<RefreshResult> future){return new RefreshResult(future);}
        public RefreshResult SC_refreshResult(int code, int shopCid, int type){return new RefreshResult(code, shopCid, type);}
        public RefreshResult SC_refreshResult(int code){return new RefreshResult(code);}
		/// <summary>
        /// 商店更新
        /// </summary>
		public bool SC_updateShop(ISession session, ShopPOD shop){
	        return NetProtocolHelper.SendMessage(session, new UpdateShop(shop));
	    }
	    public void SC_updateShop_Broadcast(List<ISession> sessions, ShopPOD shop){
	        NetProtocolHelper.BroadcastMessage(sessions, new UpdateShop(shop));
	    }
#endregion

#region Messages
	public class BuyResult : MessageResult {
	    private int shopCid;
	    private int index;
	    public BuyResult(Task<BuyResult> future): base(future) { }
		public BuyResult(int code): base(code) { }
		public new BuyResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal BuyResult(int code, int shopCid, int index){
	        this.code = code;
	        this.shopCid = shopCid;
	        this.index = index;
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<BuyResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.shop_buyResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.shop_buyResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_int(shopCid);
			_out.Write_int(index);
			return si_p;
		}
		public override string ToString() {
    		return "Shop.SC_buyResult { "
			+ "code="
			+ code
			+ ", shopCid="
			+ shopCid
			+ ", index="
			+ index
			+" }";
		}
	}
	public class RefreshResult : MessageResult {
	    private int shopCid;
	    private int type;
	    public RefreshResult(Task<RefreshResult> future): base(future) { }
		public RefreshResult(int code): base(code) { }
		public new RefreshResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal RefreshResult(int code, int shopCid, int type){
	        this.code = code;
	        this.shopCid = shopCid;
	        this.type = type;
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<RefreshResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.shop_refreshResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.shop_refreshResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_int(shopCid);
			_out.Write_int(type);
			return si_p;
		}
		public override string ToString() {
    		return "Shop.SC_refreshResult { "
			+ "code="
			+ code
			+ ", shopCid="
			+ shopCid
			+ ", type="
			+ type
			+" }";
		}
	}
	public class UpdateShop : MessageNotify {
		private ShopPOD shop;
		public UpdateShop(ShopPOD shop) {
		    this.shop = shop;
		}
		public override int GetMessageId() {return NetMessageId.shop_updateShop;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.shop_updateShop;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_pod(shop);
			return si_p;
		}
		public override string ToString() {
			return "Shop.SC_updateShop { "
			+ "shop="
			+ shop
			+" }";
		}
	}
	private class BuyRequest : IReceiveMessage
    {
		internal int shopCid;
		internal int index;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            shopCid = _in.HasRemaining() ? _in.Read_int() : default;
            index = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.shop_buy; }
        public override string ToString()
        {
            return "Shop.buy"+ ", shopCid="+ shopCid+ ", index="+ index;
        }
    }
	private class RefreshRequest : IReceiveMessage
    {
		internal int shopCid;
		internal int type;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            shopCid = _in.HasRemaining() ? _in.Read_int() : default;
            type = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.shop_refresh; }
        public override string ToString()
        {
            return "Shop.refresh"+ ", shopCid="+ shopCid+ ", type="+ type;
        }
    }
	}
#endregion
}